<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class AdminSite extends BaseModel
{
    //定义表名 工地表
    protected $name = 'longbing_card_decorate_site';
    protected $append = [
        'area_text',
        'style_text',
        'housewear_text',
        'designer_text',
        'progress_text',
    ];

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取地区名字
     */
    public function getProgressTextAttr($value,$data){
        $p_model = new AdminProgress();
        return $p_model->progressTitle(['id'=>$data['progress']]);
    }


    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取地区名字
     */
    public function getAreaTextAttr($value,$data){
        $area_model = new AdminArea();
        return $area_model->areaTitle(['id'=>$data['area']]);
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取风格名字
     */
    public function getStyleTextAttr($value,$data){
        $style_model = new AdminStyle();
        return $style_model->styleTitle(['id'=>$data['style']]);
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:57
     * @param $value
     * @param $data
     * @return mixed
     * descption:居室名字
     */
    public function getHousewearTextAttr($value,$data){
        $wear_model = new AdminHousewear();
        return $wear_model->housewearTitle(['id'=>$data['housewear']]);
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgsAttr($value){
        $img = transImages([$value],['imgs'],',');
        return is_array($img[0])?$img[0]:[];
    }
    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换vr封面图
     */
    public function getCoverAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }
    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换vr封面图
     */
    public function getVrCoverAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }
    /**
     * User: chenniang
     * Date: 2019-09-16 14:04
     * @param $value
     * @param $data
     * @return void
     * descption:获取区域
     */
    public function getAreaAttr($value,$data){
        $area_model = new AdminArea();
        return $area_model->reset($data['area']);
    }

    /**
     * User: chenniang
     * Date: 2019-10-22 14:31
     * @param $value
     * @return int
     * descption:int类型
     */
    public function getDesignerAttr($value,$data){
        $model = new AdminStaffSite();
        $staff = $model->where(['site_id'=>$data['id']])->column('staff_id');
        return array_values($staff);
    }
    /**
     * User: chenniang
     * Date: 2019-10-22 14:31
     * @param $value
     * @return int
     * descption:int类型
     */
    public function getDesignerTextAttr($value,$data){
        $data['designer']  = !empty($data['designer'])?explode(',',$data['designer']):[];
        $dis[] = ['is_staff','=',1];
        $dis[] = ['fans_id','in',$data['designer']];
        $designer = Db::name('longbing_card_user_info')->where($dis)->column('name');
        return array_values($designer);
    }
    /**
     * User: chenniang
     * Date: 2019-10-22 14:31
     * @param $value
     * @return int
     * descption:int类型
     */
    public function getOwnerAttr($value){
        return intval($value);
    }
    /**
     * User: chenniang
     * Date: 2019-10-22 14:31
     * @param $value
     * @return int
     * descption:int类型
     */
    public function getAdviserAttr($value){
        return intval($value);
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 工地列表
     */
    public function siteList($dis,$page = 10,$where){

        if(empty($where)){

            $where = [];
        }
        $data = $this->where($dis)->where($where)->status(-1)->order('sort desc,create_time desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['designer_info'] = $this->siteStaff($v['designer'],$v['uniacid']);

            }
        }

        return $data;
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 工地列表
     */
    public function siteSelect($dis,$filed='*'){
        $data = $this->where($dis)->status(-1)->order('sort desc,create_time desc')->field($filed)->select()->toArray();
        return $data;
    }
    /**
     * @param $data
     * @return int|string
     * 添加工地
     */
    public function siteAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
//        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑工地
     */
    public function siteUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * 工地详情
     */
    public function siteInfo($dis){
        $data = $this->where($dis)->status(-1)->find();
        if(!empty($data)){
            $housewear = new AdminHousewear();
            $style     = new AdminStyle();
            $data->housewear = $housewear->housewearTitle(['id'=>$data->housewear],'id');
            $data->style     = $style->styleTitle(['id'=>$data->style],'id');
        }
        return !empty($data)?$data->toArray():$data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 16:36
     * @return void
     * descption:转换数据
     */
    public function changeData($data){
        $data['area'] = !empty($data['area'])?array_pop($data['area']):'';
        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';
        $data['designer'] = !empty($data['designer'])?implode(',',$data['designer']):'';
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-23 16:24
     * @param $dis
     * @return mixed
     * descption:获取标题
     */
    public function siteTitle($dis){
        return $this->where($dis)->status(-1)->value('title');
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-18 10:02
     * @功能说明:工地员工
     */
    public function siteStaff($staff,$uniacid){

        $dis[] = ['uniacid','=',$uniacid];

        $dis[] = ['fans_id','in',$staff];

        $dis[] = ['is_staff','=',1];

        $data  = Db::name('longbing_card_user_info')->where($dis)->field('fans_id,name')->select()->toArray();

        if(!empty($data)){

            foreach ($data as $ks=>$vs){

                $data[$ks]['name'] = !empty($vs['name'])?$vs['name']:lbUserName($vs['fans_id']);

            }

        }

       return $data;
    }


}